<?php
include_once('util/bloqueDeSeguridad.php');

include_once('datos/LocalidadDAL.php');
include_once('datos/ProvinciaDAL.php');
include_once('datos/TipoDocumentoDAL.php');

$tituloSistema = "Administraci&oacute;n de Ventas";
$tituloPagina = "Gesti&oacute;n de Solicitudes";

$idEmpresa = 2;
$empresaImagen = $_SESSION["Icono"];


$loc = new LocalidadDAL();
$resultLocalidad = $loc->comboLocalidad();

$provin = new ProvinciaDAL();
$resultProv = $provin->comboProvincia();

$tipo = new TipoDocumentoDAL();
$resulTipoDNI = $tipo->comboTipoDocumento();

//Alexis 
//Info: 
// - recargar las fechas con fecha actrual para fechahasta y la fechadesde es una semana anterior.

$fechaDesde = Date('d/m/Y', strtotime("-15 days"));
$fechaHasta = Date('d/m/Y');

$pathFull = "SolicitudBLL.php?fechadesde=" . $fechaDesde . "&fechahasta=" . $fechaHasta;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title><?php echo $tituloPagina; ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <script src="Content/Scripts/jquery-1.7.2.js" type="text/javascript"></script>
        <script src="Content/Scripts/jquery-ui-1.8.20.custom.min.js" type="text/javascript"></script>
        <script src="Content/Scripts/jquery.ui.multiselect-4.3.3.js" type="text/javascript"></script>
        <!--
        <script src="Content/Scripts/jquery.jqGrid.locale-en-4.1.2.js" type="text/javascript"></script>
        //-->

        <script src="js/i18n/grid.locale-es.js" type="text/javascript"></script>

        <script src="Content/Scripts/jquery.jqGrid-4.3.3.min.js" type="text/javascript"></script>
        <script src="Content/Scripts/jquery.droppy.js" type="text/javascript"></script>
        <script src="Content/Scripts/jquery.validate.min.js" type="text/javascript"></script>
        <script src="Content/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
        <script src="js/fancybox.js" type="text/javascript"></script>
        <link href="Content/Site.css" rel="stylesheet" type="text/css" />
        <link href="Content/jquery.css" rel="stylesheet" type="text/css" />
        <link href="Content/themes/redmond/jquery-ui-1.8.20.custom.css" rel="stylesheet" type="text/css" />
        <link href="Content/jqGrid/jquery-ui-jqgrid.css" rel="stylesheet" type="text/css" /> 
        <link rel="stylesheet" media="screen" type="text/css" href="Content/smoothness.datepick.css" />
        <script type="text/javascript" src="js/jquery.datepick.js"></script>
        <script type="text/javascript" src="js/jquery.datepick-es.js" ></script>
        <script>
            $(function() {
                $('#fechadesde').datepick({showOn: 'both', dateFormat: 'dd/mm/yy', clearText: '', clearStatus: '', closeText: '', closeStatus: '', buttonImageOnly: true, buttonImage: 'Content/images/calendar-blue.gif'});
                $('#fechahasta').datepick({showOn: 'both', dateFormat: 'dd/mm/yy', clearText: '', clearStatus: '', closeText: '', closeStatus: '', buttonImageOnly: true, buttonImage: 'Content/images/calendar-blue.gif'});
            })
        </script>
        <script type="text/javascript">
            $(function() {

                $grid = $("#gridSolicitudAdmin"),
                        initDateEdit = function(elem) {
                    setTimeout(function() {
                        $(elem).datepicker({
                            dateFormat: 'dd-M-yy',
                            autoSize: true,
                            changeYear: true,
                            changeMonth: true,
                            showButtonPanel: true,
                            showWeek: true
                        });
                    }, 100);
                },
                        initDateSearch = function(elem) {
                    setTimeout(function() {
                        $(elem).datepicker({
                            dateFormat: 'dd-M-yy',
                            autoSize: true,
                            changeYear: true,
                            changeMonth: true,
                            showWeek: true,
                            showButtonPanel: true
                        });
                    }, 100);
                },
                        numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number',
                    searchoptions: {sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge', 'nu', 'nn', 'in', 'ni']}},
                dateTemplate = {width: 300, align: 'center', sorttype: 'date', formatter: 'date',
                    formatoptions: {newformat: 'd-M-Y'}, datefmt: 'd-M-Y', editoptions: {dataInit: initDateEdit},
                    searchoptions: {sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge'], dataInit: initDateSearch}},
                yesNoTemplate = {align: 'center', formatter: 'checkbox',
                    edittype: 'checkbox', editoptions: {value: 'Yes:No', defaultValue: 'Yes'},
                    stype: 'select', searchoptions: {sopt: ['eq', 'ne'], value: ':Any;true:Yes;false:No'}};

                $grid.jqGrid({
                    url: '<?php echo $pathFull; ?>',
                    datatype: "xml",
                    mtype: "POST",
                    colNames: ['Numero Solicitud', 'Empresa', 'Fecha Solicitud', 'Monto Poliza', 'Monto Pago', 'idClienteSolicitud',
                        'Usuario', 'Activo', 'Cobrado', 'Borrado', 'idCliente', 'Nombre', 'Apellido', 'Fecha Nacimiento', 'Tipo Documento', 'Nro Documento', 'Email', 'Email2',
                        'Calle', 'Numero', 'Piso', 'Depto', 'Telefono', 'Telefono2', 'Localidad', 'Provincia', 'Codigo Postal', 'Codigo Postal',
                        'idClienteTarjeta', 'Numero Tarjeta', 'Codigo Verificacion', 'Vencimiento Tarjeta', 'idEmpresaSolicitud'],
                    colModel: [
                        {name: 'numSolicitud', index: 'numSolicitud', width: 100, key: true, editable: true, editoptions: {readonly: 'readonly'}},
                        {name: 'idEmpresaSolicitud', index: 'idEmpresaSolicitud', width: 200},
                        {name: 'FechaSolicitud', index: 'FechaSolicitud', width: 300, template: dateTemplate, editable: true},
                        {name: 'MontoPoliza', index: 'MontoPoliza', width: 200, template: numberTemplate, editable: true},
                        {name: 'MontoPago', index: 'MontoPago', width: 200, template: numberTemplate, editable: true},
                        {name: 'idClienteSolicitud', index: 'idClienteSolicitud', width: 200, hidden: true},
                        {name: 'idUsuario', index: 'idUsuario', width: 200, hidden: true},
                        {name: 'isActivo', index: 'isActivo', width: 200, template: yesNoTemplate},
                        {name: 'isCobrado', index: 'isCobrado', width: 200, template: yesNoTemplate, editable: true},
                        {name: 'isBorrado', index: 'isBorrado', width: 200, hidden: true},
                        {name: 'idCliente', index: 'idCliente', width: 200, hidden: true},
                        {name: 'Nombre', index: 'Nombre', width: 200, editable: true, editrules: {required: true, minValue: 3, maxValue: 200}},
                        {name: 'Apellido', index: 'Apellido', width: 200, editable: true, editrules: {required: true, minValue: 3, maxValue: 200}},
                        {name: 'FechaNacimiento', index: 'FechaNacimiento', width: 300, template: dateTemplate, editable: true},
                        {name: 'idTipoDocumento', index: 'idTipoDocumento', width: 200, hidden: false, editoptions: {value: '<?php echo $resulTipoDNI ?>'}, editable: true, edittype: 'select', formatter: 'select'},
                        {name: 'NumeroDocumento', index: 'NumeroDocumento', width: 200, editrules: {required: true, minValue: 0, maxValue: 999999999, integer: true}, editable: true},
                        {name: 'Email', index: 'Email', width: 200, editrules: {required: false, email: true}, editable: true},
                        {name: 'Email2', index: 'Email2', width: 200, editrules: {required: false, email: true}, editable: true},
                        {name: 'Calle', index: 'Calle', width: 200, editrules: {required: true, minValue: 3, maxValue: 100}, editable: true},
                        {name: 'Numero', index: 'Numero', width: 200, editrules: {required: true, minValue: 0, maxValue: 999999999, integer: true}, editable: true},
                        {name: 'Piso', index: 'Piso', width: 200, editable: true},
                        {name: 'Depto', index: 'Depto', width: 200, editable: true},
                        {name: 'Telefono', index: 'Telefono', width: 200, editrules: {required: true, minValue: 0, maxValue: 9999999999999, integer: true}, editable: true},
                        {name: 'Telefono2', index: 'Telefono2', width: 200, editrules: {required: false, minValue: 0, maxValue: 9999999999999, integer: true}, editable: true},
                        {name: 'idLocalidad', index: 'idLocalidad', width: 200, hidden: false, editoptions: {value: '<?php echo $resultLocalidad ?>'}, editable: true, edittype: 'select', formatter: 'select'},
                        {name: 'idProvincia', index: 'idProvincia', width: 200, hidden: false, editoptions: {value: '<?php echo $resultProv ?>'}, editable: true, edittype: 'select', formatter: 'select'},
                        {name: 'CodigoPostal', index: 'CodigoPostal', width: 200, editable: true, editrules: {required: true, minValue: 3, maxValue: 100}},
                        {name: 'CodigoPostalManual', index: 'CodigoPostalManual', width: 200},
                        {name: 'idClienteTarjeta', index: 'idClienteTarjeta', width: 200, hidden: true},
                        {name: 'NumeroTarjeta', index: 'NumeroTarjeta', width: 200, editrules: {required: true, minValue: 0, maxValue: 9999999999999999, integer: true}, editable: true},
                        {name: 'CodVerificacion', index: 'CodVerificacion', width: 200, editrules: {required: true, minValue: 0, maxValue: 999999, integer: true}, hidden: true, editable: true},
                        {name: 'VencimientoTarjeta', index: 'VencimientoTarjeta', editrules: {required: true, integer: true}, width: 200, editable: true},
                        {name: 'idEmpresaSolicitud', index: 'idEmpresaSolicitud', width: 200, hidden: true}
                    ],
                    rowNum: 20,
                    rowList: [5, 10, 20, 50, 100, 500, 1000],
                    pager: '#pager',
                    gridview: true,
                    rownumbers: true,
                    autoencode: true,
                    ignoreCase: true,
                    sortname: 'Empresa',
                    viewrecords: true,
                    multiselect: true,
                    sortorder: 'desc',
                    shrinkToFit: true,
                    height: '100%',
                    width: '920'
                });

                $("#solModificar").click(function() {

                    var gr = jQuery("#gridSolicitudAdmin").jqGrid('getGridParam', 'selarrrow');

                    if (gr != null && gr.length == 1) {

                        $.fancybox({
                            maxWidth: 800,
                            //maxHeight	: 600,
                            fitToView: false,
                            width: '80%',
                            height: 650,
                            autoSize: false,
                            openEffect: 'fade',
                            closeEffect: 'none',
                            type: 'iframe',
                            closeClick: true,
                            href: 'SolicitudModificar.php?modi=' + gr
                        });
                    }
                    else
                        alert("Por favor seleccione una solicitud");
                });

                $('#Filtrar').click(function() {
                    var fechadesde = jQuery("#fechadesde").val();
                    var fechahasta = jQuery("#fechahasta").val();
                    jQuery("#gridSolicitudAdmin").jqGrid('setGridParam', {url: "SolicitudBLL.php?fechadesde=" + fechadesde + "&fechahasta=" + fechahasta, page: 1}).trigger("reloadGrid");
                });

                $('#Recargar').click(function() {
                    jQuery("#fechadesde").val('');
                    jQuery("#fechahasta").val('');
                    jQuery("#gridSolicitudAdmin").jqGrid('setGridParam', {url: "SolicitudBLL.php", page: 1}).trigger("reloadGrid");
                });

                $('#SolicitudExportarMet').click(function() {
                    var gr = jQuery("#gridSolicitudAdmin").jqGrid('getGridParam', 'selarrrow');

                    if (gr != null && gr.length > 0) {
                        //window.location.href = "SolicitudExportarMet.php?array=" + gr;
                        //makeFrame("SolicitudExportarMet.php?array=" + gr);
                        //makeFrame("SolicitudExportarMetTxt.php?array=" + gr);
                        $.fancybox({
                            maxWidth: 500,
                            //maxHeight	: 600,
                            fitToView: false,
                            width: '50%',
                            height: 450,
                            autoSize: false,
                            openEffect: 'fade',
                            closeEffect: 'none',
                            type: 'iframe',
                            closeClick: true,
                            href: 'ExportarMet.php?array=' + gr
                        });                        
                    }
                    else
                        alert("Por favor seleccione una solicitud");
                });
                
                $('#SolicitudExportarSmg').click(function() {
                    var gr = jQuery("#gridSolicitudAdmin").jqGrid('getGridParam', 'selarrrow');

                    if (gr != null && gr.length > 0) {
                        //makeFrame("SolicitudExportSmgTxt.php?array=" + gr);
                        //makeFrame("SolicitudExportarSmg.php?array=" + gr);
                        $.fancybox({
                            maxWidth: 500,
                            //maxHeight	: 600,
                            fitToView: false,
                            width: '50%',
                            height: 450,
                            autoSize: false,
                            openEffect: 'fade',
                            closeEffect: 'none',
                            type: 'iframe',
                            closeClick: true,
                            href: 'ExportarSMG.php?array=' + gr
                        });                         
                    }
                    else
                        alert("Por favor seleccione una solicitud");
                });   
                
                function makeFrame( url ) 
                    { 
                        ifrm = document.createElement( "IFRAME" ); 
                        ifrm.setAttribute( "style", "display:none;" ) ;
                        ifrm.setAttribute( "src", url ) ; 
                        ifrm.style.width = 0+"px"; 
                        ifrm.style.height = 0+"px"; 
                        document.body.appendChild( ifrm ) ; 
                    }  ;

            });
            //]]>
        </script>   
    </head>
    <body>
        <div class="page">
            <?php require_once('util/header.php'); ?>          
            <div id="main">   
                <h2><?php echo $tituloPagina; ?></h2>
                <div align="center">
                    <div id="fechas">
                        <div style="float:left">Fecha desde<br /><input type="text" id="fechadesde" value="<?php echo $fechaDesde; ?>" /></div>
                        <div style="float:left;margin-left:15px"> Fecha hasta<br /><input type="text" id="fechahasta" value="<?php echo $fechaHasta; ?>"/>
                            <input type="submit" value="Filtrar" id="Filtrar"  class="boton" style="margin-left:15px;"/>
                            <input type="submit" value="Recargar" id="Recargar"  class="boton" style="margin-left:15px;"/>
                        </div>
                        <div style="clear:both"> </div>
                    </div>
                    <table id="gridSolicitudAdmin"><tr><td></td></tr></table>
                    <div id="pager"></div>
                    <div id="botones">
                        <a href="SolicitudAlta.php" class="abrir boton">Nueva</a>&nbsp;
                        <a id="solModificar" class="modificacionAbrirJ boton">Modificar</a>
                        <a id="SolicitudExportarMet" class="SolicitudExportarMet boton">ExportarMet</a>
                        <a id="SolicitudExportarSmg" class="modificacionAbrirJ boton">ExportarSMG</a>
                    </div>
                </div>
            </div>
            <?php require_once('util/piepagina.php'); ?>
        </div>
    </body>
    <script type="text/javascript">
        $(document).ready(function() {
            $(".abrir").fancybox({
                maxWidth: 800,
                //maxHeight	: 600,
                fitToView: false,
                width: '80%',
                height: 650,
                autoSize: false,
                openEffect: 'fade',
                closeEffect: 'none',
                type: 'iframe',
                closeClick: true
            });
        });
    </script>
</html>
